package com.kafkacode.producer;

import com.kafkacode.model.User;
import com.kafkacode.utils.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class EventProducer {

    //加入了spring-kafka依赖 + .yml配置信息，springboot自动配置好了kafka，自动装配好了KafkaTemplate这个Bean
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendEvent() {
        User user = User.builder().id(1028).phone("13709090901").birthDay(new Date()).build();
        String userJSON = JSONUtils.toJSONString(user);
        kafkaTemplate.send("intTopic", "k", userJSON);
    }
}

